<template>
    <div class="login-container">
        <div class="log_box">
            <el-form ref="loginFromRef" class="login_from" :model="loginForm" :rules="loginFromrules">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input placeholder="请输入用户名" prefix-icon="iconfont icon-wo" v-model="loginForm.username">
                    </el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" prefix-icon="iconfont icon-mima" v-model="loginForm.password" type="password">
                    </el-input>
                </el-form-item>

                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginFrom">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return {
           //登录表单的数据绑定对象
           loginForm:{
               username:'admin',
               password:'123456'
           },
           //表单验证规则对象
           loginFromrules:{
               //验证用户名
               username:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
               ],
               //验证密码
               password:[
                    { required: true, message: '请输入登录密码', trigger: 'blur' },
                    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
               ]
           }
        }
    },
    methods:{
            //点击重置按钮，重置登录表单
        resetLoginFrom(){
            // console.log(this);
            this.$refs.loginFromRef.resetFields();
        },
        login(){
            this.$refs.loginFromRef.validate(async valid =>{
                if(!valid) return;
                const {data:res}=await this.$http.post('login',this.loginForm)
                if(res.meta.status !=200) return this.$message.error('登录失败，请核对信息！');
                this.$message.success('登录成功')
                //将登录成功之后的token保存到客户端的sessionStorage中
                window.sessionStorage.setItem("token",res.data.token);
                //通过编程式导航跳转到后台主页
                this.$router.push("/home");
            })
        }
    }
}
</script>

<style lang="less" scoped>

.login-container{
    background-color: #2b4b6b;
    height: 100%;
}
.log_box{
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
}
.login_from{
    //默认的表单为box-sizing: content-box;
    box-sizing: border-box;
    margin-top: 70px;
    padding: 0 15px;
}
.btns{
    //弹性框模型
    display: flex;
    //横轴居右对齐
    justify-content: flex-end;
}
</style>